from rest_framework import serializers
from goods import models

def get(value):
    name = models.Goods.objects.filter(name=value)
    if len(name) > 0:
        data = name[0]
        data.number += 1
        data.save()
        raise serializers.ValidationError('商品名称不能重复')
    return value

class Goods(serializers.ModelSerializer):
    class Meta:
        model = models.Goods
        fields = '__all__'
        extra_kwargs = {
            "name": {"validators": [get]}
        }

class Shop(serializers.ModelSerializer):
    class Meta:
        model = models.Shop
        fields = '__all__'

